<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('encrypt');
        $this->load->model('AdminModel');
        $this->load->model('PublicModel');
    }

    /**
     * 后台首页
     */
    public function index() {
        $sess_data = $this->session->userdata();
        isset($sess_data['is_login']) ? $is_login = $sess_data['is_login'] : $is_login = FALSE;
        if ($is_login) {
            $data = $this->PublicModel->getPublicInfo($this->session->userdata('uid'));
            $this->session->set_userdata('admin_public', $data['name']);
            redirect('/console/dashboard');
        } else {
            redirect('/welcome/login');
        }
    }

    /**
     * 用户登录
     */
    public function login() {

        $hour = date('H');
        if ($hour <= 11) {
            $welcome_msg = '早上好，欢迎。';
        } else if (11 < $hour && $hour <= 13) {
            $welcome_msg = '中午好，欢迎。';
        } else if (13 < $hour && $hour <= 17) {
            $welcome_msg = '下午好，欢迎。';
        } else if ($hour > 17) {
            $welcome_msg = '晚上好，欢迎。';
        }

        $data['welcome_msg'] = $welcome_msg;
        $data['bgimg_num'] = mt_rand(0, 5);
        $data['page_title'] = '登录';

        $this->form_validation->set_rules('account', '用户名', 'trim|required');
        $this->form_validation->set_rules('password', '密码', 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('/admin/welcome/login', $data);
        } else {
            $account = $this->input->post('account');
            $password = $this->input->post('password');
            if ($this->AdminModel->isAccount($account, $password)) {
                redirect('/welcome/index');
            } else {
                $data['error_msg'] = '用户名或密码错误';
                $this->load->view('/admin/welcome/login', $data);
            }
        }
    }

    /**
     * 用户注销
     */
    public function logout() {
        $this->session->sess_destroy();
        redirect('/welcome/index/');
    }

    /**
     * 新用户注册
     */
    public function regist() {

        $data['page_title'] = '注册';
        $data['bgimg_num'] = mt_rand(0, 5);

        $this->form_validation->set_rules('account', '用户名', 'trim|required|min_length[5]|max_length[12]|callback_account_unique', array(
            'required' => '用户名不能为空',
            'min_length[5]' => '用户名至少需要5位',
            'max_length[12]' => '用户名最大为12位',
                )
        );
        $this->form_validation->set_rules('password', '密码', 'trim|required|min_length[8]', array(
            'required' => '密码不能为空',
            'min_length' => '密码至少需要8位',
                )
        );
        $this->form_validation->set_rules('passconf', '确认密码', 'trim|required|matches[password]', array(
            'required' => '两次密码输入不相同',
            'matches' => '两次密码输入不相同',
                )
        );
        $this->form_validation->set_rules('agree', '确认密码', 'callback_agree_check', array(
            'required' => '两次密码输入不相同',
            'matches' => '两次密码输入不相同',
                )
        );

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('/admin/welcome/regist', $data);
        } else {
            $new_admin['admin_account'] = $this->input->post('account');
            $new_admin['admin_password'] = $this->encrypt->encode($this->input->post('password'));
            $this->AdminModel->add($new_admin);
            redirect('/welcome/login');
        }
    }

    /**
     * 检测用户名是否存在
     * @param String $account 用户名
     * @return boolean
     */
    public function account_unique($account) {
        if ($this->AdminModel->is_unique($account)) {
            $this->form_validation->set_message('account_unique', '用户名已存在');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    /**
     * 判断用户是否同意协议
     * @param Int $agree
     * @return boolean
     */
    public function agree_check($agree) {
        if ($agree == 1) {
            return TRUE;
        } else {
            $this->form_validation->set_message('agree_check', '您还未同意用户协议');
            return FALSE;
        }
    }

}
